package com.example.myproject.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;


public class JsonUtil {
	
	private static final String JACKSON_OBJECT_MAPPER_BEAN_NAME = "jacksonObjectMapper";// jackson ObjectMapper Bean名称

	public static ObjectMapper getMapper() {
		ObjectMapper c = new ObjectMapper();
		c.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		c.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
		//c.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//jackson默认写出的时间数据为时间戳， 这里修改为相应模式的时间数据输出格式
	    // c.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		return c ;
	}
	
	// 将对象转换为JSON字符串
	public static String toJson(Object object) {
		ObjectMapper mapper = getMapper();
		try {
			return mapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String serializeAllExcept(Object obj, String... filterFields)
	{
		try
		{
			ObjectMapper mapper = getMapper();
			FilterProvider filters = new SimpleFilterProvider().addFilter(obj.getClass().getName(), SimpleBeanPropertyFilter.serializeAllExcept(filterFields));
			mapper.setFilters(filters);
			mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector()
			{
				@Override
				public Object findFilterId(Annotated ac)
				{
					return ac.getName();
				}
			});
			return mapper.writeValueAsString(obj);
		}
		catch (Exception e)
		{}
		return null;
	}

	
	// 将JSON字符串转换为对象
	public static <T> T toObject(String json, Class<T> clazz) {
		ObjectMapper mapper = getMapper();
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static <T> T toObject(String json, com.fasterxml.jackson.core.type.TypeReference typeReference) {
		ObjectMapper mapper = getMapper();
		try {
			return mapper.readValue(json, typeReference);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	//把json 字符串转化成复杂对象的手
	/**
	 * ArrayList<YourBean> getTypeFactory().constructParametricType(collectionClass, elementClasses);
	 * HashMap<String,YourBean> getTypeFactory().constructParametricType(HashMap.class,String.class, YourBean.class);
	 * @param json
	 * @param collectionClass
	 * @param elementClasses
	 * @return
	 */
	public static Object toObjectByCollectionType(String json,Class<?> collectionClass, Class<?>... elementClasses){
		ObjectMapper mapper = getMapper();
		try {
			return mapper.readValue(json, getCollectionType(collectionClass,elementClasses));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 先反序列化复杂类型 为泛型的Collection Type
	 * @param collectionClass
	 * @param elementClasses
	 * @return
	 */

	private static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		ObjectMapper mapper = getMapper();
		return mapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
	}


}